package com.liuugoqing.behavioral.visitor.doubledispatch;

import org.junit.Test;

/**
 * 所谓双分派技术就是在选择一个方法的时候，不仅仅要
 * 根据消息接收者（receiver）的运行时区别，还要根据参数的运行时区别。
 * @Author : liugq
 * @Date : 2022/7/15 21:47
 * @See : 参考类
 * @Since： JDK1.8
 * @Version : 1.0
 */
public class Client {
    public class Animal {
        public void accept(Execute exe) {
            exe.execute(this);
        }
    }

    public class Dog extends Animal {
        public void accept(Execute exe) {
            exe.execute(this);
        }
    }

    public class Cat extends Animal {
        public void accept(Execute exe) {
            exe.execute(this);
        }
    }

    public class Execute {
        public void execute(Animal a) {
            System.out.println("animal");
        }

        public void execute(Dog d) {
            System.out.println("dog");
        }

        public void execute(Cat c) {
            System.out.println("cat");
        }
    }

    @Test
    public void test() throws Exception{
        Animal a = new Animal();
        Animal d = new Dog();
        Animal c = new Cat();

        Execute exe = new Execute();
        a.accept(exe);
        d.accept(exe);
        c.accept(exe);
    }
}
